package com.yungu.swift.api.route.v2.common;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.annotation.ResponseImgOverride;
import com.yungu.swift.order.model.param.ApiOrderPayParam;
import com.yungu.swift.order.model.param.ApiOrderUuidParam;
import com.yungu.swift.order.model.param.SecretNoParam;
import com.yungu.swift.order.model.vo.CrosstownFareInfoVo;
import com.yungu.swift.order.model.vo.JsonOrderDetailVo;
import com.yungu.swift.order.model.vo.OrderEstimateVo;
import com.yungu.swift.order.model.vo.TripSharingInfoVo;
import com.yungu.swift.order.service.*;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.yungu.swift.api.base.ApiErrorEnum.PARAM_ERROR;
import static com.yungu.swift.order.model.enums.OrderErrorEnum.GRAB_ERROR_4017;

/**
 * @author : cuixiuyin
 * @date : 2019/11/21
 */
@RestController
@RequestMapping("/api/v2/common")
@Api(value = "【通用】【行程】通用行程接口V2", tags = "V2-通用行程接口")
public class CommonTripApi {

    @Reference
    private OrderService orderService;
    @Reference
    private OrderFareService orderFareService;
    @Reference
    private OrderSecretNoService orderSecretNoService;
    @Reference
    private OrderTripSharingService orderTripSharingService;
    @Reference
    private OrderRiskControlService orderRiskControlService;

    @ResponseImgOverride
    @PostMapping(value = "/tripSharing")
    @ApiOperation(value = "行程分享接口")
    public ResponseData<TripSharingInfoVo> tripSharing(@RequestHeader String appid,
                                                       @RequestBody ApiOrderUuidParam orderUuidParam) {
        return orderTripSharingService.tripSharingInfo(orderUuidParam.getOrderUuid(), appid);
    }

    @PostMapping(value = {"/privacyNum/queryBinding"})
    @ApiOperation(value = "查询隐私号绑定")
    public ResponseData<Map<String, String>> queryBinding(@RequestBody SecretNoParam secretNoParam) {
        if (ParamUtil.isIllegal(secretNoParam.getPassengerMobile(), secretNoParam.getOrderUuid())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return orderSecretNoService.queryBinding(secretNoParam);
    }

    @PostMapping(value = {"/order/getRealtimeFare"})
    @ApiOperation(value = "乘客/司机获取订单的实时价格和时间", notes = "订单实时价格")
    public ResponseData<OrderEstimateVo> getRealtimeFare(@RequestBody ApiOrderUuidParam orderUuidParam) {
        if (ParamUtil.isIllegal(orderUuidParam)) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return orderFareService.getRealtimeFare(orderUuidParam);
    }


    @PostMapping(value = {"/token/order/fareItems"})
    @ApiOperation(value = "乘客/司机订单费用详情接口", notes = "费用详情")
    public ResponseData<Map<String, Object>> fareItems(@RequestBody ApiOrderPayParam orderPayParam) {
        if (ParamUtil.isIllegal(orderPayParam.getOrderUuid())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return orderFareService.getFareItems(orderPayParam.getOrderUuid(), orderPayParam.getCouponUuid(),1);
    }

    @PostMapping(value = {"/token/crosstown/order/fareItems"})
    @ApiOperation(value = "乘客/司机跨城订单费用详情接口", notes = "费用详情")
    public ResponseData<CrosstownFareInfoVo> fareItems(@RequestBody Map<String, Object> map) {
        Object orderUuid = map.get("orderUuid");
        Object identity = map.get("identity");
        if (ParamUtil.isIllegal(orderUuid, identity)) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return orderFareService.crosstownFareItems(String.valueOf(orderUuid), Integer.valueOf(identity.toString()));
    }

    @PostMapping(value = {"/token/order/info"})
    @ApiOperation(value = "乘客/司机获取订单详情接口", notes = "订单详情")
    public ResponseData<JsonOrderDetailVo> info(@RequestBody Map<String, String> paramMap) {
        // 订单 uuid
        String orderUuid = paramMap.get("orderUuid");
        // 司机 uuid
        String driverUuid = paramMap.get("driverUuid");
        if (StringUtils.isEmpty(orderUuid)) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        if (StringUtils.isNotEmpty(driverUuid) && !orderRiskControlService.isValidOrder(orderUuid, driverUuid).getData()) {
            return ResponseData.buildErrorResponse(GRAB_ERROR_4017.getCode(), GRAB_ERROR_4017.getMsg());
        }
        return orderService.getOrderDetail(orderUuid, null);
    }
}
